<?php

class DbRepository
{
	protected $con;
	
	
	
	public function __construct($con)
	{
		$this->setConnection($con);
	}
	
	
	
	public function setConnection($con)
	{
		$this->con = $con;
	}
	
	
	
	public function execute($sql, $params = array())
	{
		$this->setUtf();
		$stmt = $this->con->prepare($sql);
		$stmt->execute($params);
		
		return $stmt;
	}
	
	
	
	public function fetch($sql,$params = array())
	{
		return $this->execute($sql, $params)->fetch(PDO::FETCH_ASSOC);
	}
	
	
	
	public function fetchAll($sql,$params = array())
	{
		return $this->execute($sql, $params)->fetchAll(PDO::FETCH_ASSOC);
	}
	
	
	
	public function setUtf()
	{
		$sql = "SET NAMES UTF8";
		$stmt = $this->con->prepare($sql);
		$stmt->execute();
	}
	
	
	
	public function lockTable($table_name)
	{
		$sql = "LOCK TABLES $table_name WRITE";
		$this->execute($sql);
	}
	
	
	
	public function unlockTable()
	{
		$sql = "UNLOCK TABLES";
		$this->execute($sql);
	}
	
	
	
	public function hashPassword($password)
	{
		return sha1($password . 'UniqueKeyIsNothing');
	}
}